<template>
  <div class="carousel">
    <div
      class="carousel-item"
      v-for="(item, index) in carouselItems"
      :key="index"
      :style="{ backgroundImage: `url(${item.image})` }"
      :class="{ active: currentIndex === index }"
    >
    </div>
    <div class="carousel-nav">
      <button
        v-for="(_, navIndex) in carouselItems"
        :key="navIndex"
        :class="{ 'nav-active': currentIndex === navIndex }"
        @click="currentIndex = navIndex"
      >
      </button>
    </div>
  </div>
</template>

<script>
import { ref, onUnmounted } from 'vue'

export default {
  setup(props) {
    const currentIndex = ref(0)
    const nextIndex = () => {
      currentIndex.value = (currentIndex.value + 1) % props.carouselItems.length
    }
    const autoSlide = setInterval(nextIndex, 3000)

    onUnmounted(() => {
      clearInterval(autoSlide)
    })

    return {
      currentIndex
    }
  },
  props: {
    carouselItems: {
      type: Array,
      required: true
    }
  }
}
</script>

<style scoped>
.carousel {
  width: 100%;
  height: 450px;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.carousel-item {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  position: absolute;
  opacity: 0;
  transition: opacity 1s ease;
}

.carousel-item.active {
  opacity: 1;
}

.carousel-nav {
  position: absolute;
  bottom: 15px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  justify-content: center;
}

.carousel-nav button {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: none;
  background-color: rgba(255, 255, 255, 0.5);
  margin: 0 8px;
  cursor: pointer;
  outline: none;
}

.carousel-nav button.nav-active {
  background-color: white;
}

@media (max-width: 768px) {
  .carousel {
    height: 300px;
  }
}
</style>